home *** CD-ROM | disk | FTP | other *** search
- Path: news.cencom.net!ns!tanp
- From: tanp@ns (Bill Wendling)
- Newsgroups: comp.lang.c
- Subject: Re: Why does this work?
- Date: 21 Feb 1996 07:02:53 GMT
- Organization: Cen-Com Internet
- Message-ID: <4geg2t$k3f@news.cencom.net>
- References: <4g8f7o$adt@ncar.ucar.edu>
- NNTP-Posting-Host: ns.cencom.net
- X-Newsreader: TIN [version 1.2 PL2]
-
- Jim Rosinski inexplicably wrote:
-
- } Could someone please explain why the following code works? In particular, I
- } am perplexed as to why "sub1" and "sub2" declared as elements of "cmndtable"
- } are valid "pointers to function returning void" (i.e. see the definition of
- } "cmndstruct"). When executed, the net result is indeed to invoke two
- } functions, "sub1", and "sub2" (tested on Solaris, AIX, and UNICOS).
-
- } #include <stdio.h>
- } main()
- } {
- } void sub1(), sub2();
-
- } struct cmndstruct {
- } void (*funcnam)();
- } };
-
- } struct cmndstruct cmndtable[] = {
- } sub1,
- } sub2,
- } NULL
- } };
- /*
- sub1 and sub2 are the reference to the functions that they call. This has
- the same effect of doing:
-
- void sub1();
- void (*funcnam)();
-
- funcnam = sub1;
-
- This is exactly how you assign a function to a function pointer.
-
- */
-
- } struct cmndstruct *cmndptr;
-
- } for (cmndptr = cmndtable; *(cmndptr->funcnam) != NULL; cmndptr++) {
- } (*(cmndptr->funcnam))();
- } }
- } exit(0);
- } }
-
- } void sub1()
- } {
- } printf("Inside sub1\n");
- } }
-
- } void sub2()
- } {
- } printf("Inside sub2\n");
- } }
-
-
- --
- Bill Wendling | "Pinky, are you thinking what I'm thinking?"
- tanp@ns.cencom.net | "I think so, Brain, but burlap chafes me so."
- "Boom Shanka" | Finger me for my Geek Code...NOW!
-